<template>
  <div class="user-profile">
    <van-nav-bar title="个人信息"
                 class="page-nav-bar"
                 left-arrow
                 @click-left="$router.back()" />
    <van-cell title="头像"
              value="内容"
              center
              is-link
              @click="$refs.file.click()">
      <van-image width="30"
                 height="30"
                 class="avatar"
                 round
                 :src="user.photo" />
    </van-cell>
    <input type="file"
           hidden
           ref="file"
           accept="image/*"
           @change="onFileChange">
    <van-cell title="昵称"
              :value="user.name"
              is-link
              @click="isUpdateNameShow = true"></van-cell>
    <van-cell title="性别"
              :value="user.gender === 0 ? '男' : '女'"
              is-link
              @click="isUpdateGenderShow = true"></van-cell>
    <van-cell title="生日"
              :value="user.birthday"
              is-link
              @click="isUpdateBirthdayShow = true"></van-cell>

    <van-popup v-model="isUpdateNameShow"
               v-if="isUpdateNameShow"
               position="bottom"
               style="height: 100%;">
      <update-name v-model="user.name"
                   @close="isUpdateNameShow = false"></update-name>
    </van-popup>

    <van-popup v-model="isUpdateGenderShow"
               v-if="isUpdateGenderShow"
               position="bottom">
      <update-gender @close="isUpdateGenderShow = false"
                     v-model="user.gender"></update-gender>
    </van-popup>

    <van-popup v-model="isUpdateBirthdayShow"
               v-if="isUpdateBirthdayShow"
               position="bottom">
      <update-birthday v-model="user.birthday"
                       @close="isUpdateBirthdayShow = false"></update-birthday>
    </van-popup>

    <van-popup v-model="isUpdatePhotoShow"
               v-if="isUpdatePhotoShow"
               position="bottom"
               style="height: 100%;">
      <update-photo :img="img"
                    @close="isUpdatePhotoShow = false"
                    @update-photo="updatephoto"></update-photo>
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import UpdateName from './components/update-name.vue'
import UpdateGender from './components/update-gender.vue'
import UpdateBirthday from './components/update-birthday.vue'
import UpdatePhoto from './components/update-photo'
export default {
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto
  },
  data () {
    return {
      user: {},
      isUpdateNameShow: false,
      isUpdateGenderShow: false,
      isUpdateBirthdayShow: false,
      isUpdatePhotoShow: false,
      img: null
    }
  },
  created () {
    this.loadUserProfile()
  },
  methods: {
    async loadUserProfile () {
      try {
        const { data } = await getUserProfile()
        this.user = data.data
        // console.log(data)
      } catch (err) {
        this.$toast('数据获取失败')
      }
    },
    onFileChange () {
      const file = this.$refs.file.files[0]
      this.img = window.URL.createObjectURL(file)
      this.isUpdatePhotoShow = true
      // 避免选择同样的文件不会触发
      this.$refs.file.value = ''
    },
    updatephoto ($event) {
      // console.log($event)
      this.user.photo = $event
    }
  }
}
</script>

<style lang="less" scoped>
.user-profile {
  .avatar {
    height: 60px;
    width: 60px;
  }
  .van-popup {
    background-color: #f5f5f5;
  }
}
</style>
